//
//  ChangeBankCardVC.m
//  Xspcf
//
//  Created by 赵欢 on 2018/4/18.
//  Copyright © 2018年 小小算盘. All rights reserved.
//

#import "ChangeBankCardVC.h"
#import "SunBankListVC.h"
#import "SunBindSuccessVC.h"
#import "SunWebVC.h"
#import "SunDelegateWebVC.h"

@interface ChangeBankCardVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tb;
    NSDictionary *_bank;
    UITextField *_code;
    UIView *_bankView;
    UIImageView *_img;
    UILabel *_bankName;
    UILabel *_name;
    UILabel *_label;
    UIView *_validateView;
    NSTimer *_timer;
    int _time;
    UIButton *_delegate;
}
@property (nonatomic,strong) UITextField *bankPhone;

@end

@implementation ChangeBankCardVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"换绑银行卡";
    self.view.backgroundColor = BACKGROUND;
    [self initInput];
    [self creatUI];
    // Do any additional setup after loading the view.
}

- (void)initInput{
    NSDictionary *dic = [Sun getUser:USER];

    _name = [[UILabel alloc]init];
    _name.font = [UIFont systemFontOfSize:16];
    _name.textColor = BLACK;
    _name.text = [dic objectForKey:@"name"];

    _bankView = [[UIView alloc]init];
    _img = [[UIImageView alloc]init];
    _img.contentMode = UIViewContentModeScaleAspectFit;
    [_bankView addSubview:_img];
    [_img lt_setLeft:X(0) to:nil save:YES];
    [_img lt_YcenterTo:nil save:YES];
    [_img lt_setSize:CGSizeMake(X(30), Y(30)) save:YES];
    _bankName = [[UILabel alloc]init];
    _bankName.font = [UIFont systemFontOfSize:16];
    _bankName.textColor = BLACK;
    [_bankView addSubview:_bankName];
    [_bankName lt_YcenterTo:nil save:YES];
    [_bankName lt_setLeft:X(2) to:_img save:YES];
    _label = [[UILabel alloc]init];
    _label.text = @"请选择银行";
    _label.font = [UIFont systemFontOfSize:16];
    _label.textColor = GRAY;
    [_bankView addSubview:_label];
    // 预留手机号TextField
    self.bankPhone = [[UITextField alloc]init];
    self.bankPhone.font = [UIFont systemFontOfSize:16];
    self.bankPhone.textColor = BLACK;
    self.bankPhone.keyboardType = UIKeyboardTypeNumberPad;
    self.bankPhone.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.bankPhone.placeholder = @"请输入银行预留手机号码";
    // 预留手机号码提示按钮
    UIButton *ques = [[UIButton alloc]init];
    [self.bankPhone addSubview:ques];
    [ques lt_setRight:X(25) to:nil save:YES];
    [ques lt_setSize:CGSizeMake(X(24), X(24)) save:YES];
    ques.layer.cornerRadius = X(12);
    [ques lt_YcenterTo:nil save:YES];
    ques.clipsToBounds = YES;
    [ques setBackgroundColor:RED forState:UIControlStateNormal];
    [ques setBackgroundColor:[HXColor colorWithHexString:@"ef4961"] forState:UIControlStateHighlighted];
    [ques setTitle:@"?" forState:UIControlStateNormal];
    [ques setTitleColor:WHITE forState:UIControlStateNormal];
    [ques addTarget:self action:@selector(question) forControlEvents:UIControlEventTouchUpInside];
    

    
}
// 预留手机号提示框
- (void)question{
    [[Sun shareSun]alertAlign:NSTextAlignmentLeft title:@"预留手机号码说明" content:@"1、银行预留手机号码是办理该银行卡时所填写的手机号码。\n2、没有预留、手机号码忘记或者已停用，请联系银行客服更新处理。" left:@"知道了" right:nil cancel:^{
        [[Sun shareSun]hideAll];
    } sure:nil To:self];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_tb reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)creatUI{
    UIView *msg = [[UIView alloc]init];
    [self.view addSubview:msg];
    [msg lt_setTop:0 to:nil save:YES];
    [msg lt_setLeft:0 to:nil Right:0 to:nil save:YES];
    msg.backgroundColor = [HXColor colorWithHexString:@"FAEFBF"];
    UILabel *red = [[UILabel alloc]init];
    [msg addSubview:red];
    [red lt_setSize:CGSizeMake(X(18), X(18)) save:YES];
    [red lt_setTop:Y(12) to:nil save:YES];
    [red lt_setLeft:X(16) to:nil save:YES];
    red.clipsToBounds = YES;
    red.backgroundColor = RED;
    red.text = @"!";
    red.font = [UIFont boldSystemFontOfSize:14];
    red.textAlignment = NSTextAlignmentCenter;
    red.textColor = WHITE;
    red.layer.cornerRadius = X(9);
    UILabel *content = [[UILabel alloc]init];
    [msg addSubview:content];
    [content lt_setTop:Y(12) to:nil save:YES];
    [content lt_setLeft:X(44) to:nil Right:X(30) to:nil save:YES];
    [content lt_layoutView:content side:SunBottom to:msg toSide:SunBottom withSize:1 offset:Y(-8) save:YES];
    content.textColor = GRAY;
    content.font = [UIFont systemFontOfSize:12];
    content.text = [NSString stringWithFormat:@"根据国家监管要求，小算盘财富已接入恒丰银行存管系统。用户实名认证，绑定银行卡，投资，充值，提现需先开通银行存管账户。存管账户为您的个人实名电子账户，平台无法触碰您的资金。"];
    content.numberOfLines = 0;
    _tb = [[UITableView alloc]initWithFrame:CGRectNull style:UITableViewStyleGrouped];
    [self.view addSubview:_tb];
    [_tb lt_setTop:0 to:msg save:YES];
    [_tb lt_setLeft:0 to:nil Right:0 to:nil save:YES];
    [_tb lt_setBottom:0 to:nil save:YES];
    _tb.backgroundColor = BACKGROUND;
    _tb.scrollEnabled = NO;
    _tb.delegate = self;
    _tb.dataSource = self;
}

- (void)requestToOpen{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_CHANGEBANKCARD];
    NSDictionary *param = @{@"phone":self.bankPhone.text,@"device":@"iOS"};
    [[Sun shareSun]showActivity:self];
    // 加密
    NSString *str = [Sun stringFromDictionary:(NSDictionary *)param];
    // 界面
    SunDelegateWebVC *vc = [[SunDelegateWebVC alloc]init];
    vc.params = str;
    vc.urlStr = url;
    vc.type = OpenBind;
    vc.title = @"更换银行卡";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)requestToUser{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_USERINFO];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        [Sun setUser:requestDic forKey:USER];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]hideActivity];
        [[Sun shareSun]showMsg:errorInfo];
    }];
}

- (void)hide{
    [[Sun shareSun]hideShow];
}

- (void)getCode{
    if (!_bank) {
        [[Sun shareSun]showMsg:@"请选择银行"];
        return;
    }
    if (self.bankPhone.text.length != 11) {
        [[Sun shareSun]showMsg:@"手机号格式有误"];
    }
    [self requestToOpen];
}

#pragma mark tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else {
        return 2;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return Y(50);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }else{
        return Y(50);
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }else {
        return 300;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"third_add_1"];
            [cell addSubview:_name];
            [_name lt_removeAllCons];
            [_name lt_YcenterTo:nil save:YES];
            [_name lt_setLeft:X(60) to:nil save:YES];
            [_name lt_setWidth:X(300) save:YES];
        }

    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"third_add_3"];
            if (_bank) {
                if (_label){
                    [_label removeFromSuperview];
                }
                [cell addSubview:_bankView];
                [_bankView lt_removeAllCons];
                [_bankView lt_YcenterTo:nil save:YES];
                [_bankView lt_setLeft:X(60) to:nil save:YES];
                [_bankView lt_setWidth:X(300) save:YES];
            }else {
                if (_bankView){
                    [_bankView removeFromSuperview];
                }
                [cell addSubview:_label];
                [_label lt_removeAllCons];
                [_label lt_YcenterTo:nil save:YES];
                [_label lt_setLeft:X(60) to:nil save:YES];
                [_label lt_setWidth:X(300) save:YES];
            }
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleDefault;
        }else if (indexPath.row == 1) {
            cell.imageView.image = [UIImage imageNamed:@"third_add_5"];
            [cell addSubview:self.bankPhone];
            [self.bankPhone lt_removeAllCons];
            [self.bankPhone lt_YcenterTo:nil save:YES];
            [self.bankPhone lt_setLeft:X(60) to:nil save:YES];
            [self.bankPhone lt_setWidth:X(300) save:YES];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = BACKGROUND;
        UILabel *label = [[UILabel alloc]init];
        [view addSubview:label];
        [label lt_setLeft:X(16) to:nil save:YES];
        [label lt_setTop:Y(10) to:nil save:YES];
        label.font = [UIFont systemFontOfSize:14];
        [label setMarkup:@"<font color=\"#777777\">请填写本人<font color=\"#66b2f3\">真实信息</font>，核实后不可更改</font>"];
        return view;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 1) {
        UIView *view = [[UIView alloc]init];
        UIButton *btn = [[UIButton alloc]init];
        [view addSubview:btn];
        [btn lt_Top:SunEdgesMake(Y(23), X(15), X(15), Y(44)) to:nil save:YES];
        btn.clipsToBounds = YES;
        [btn setBackgroundColor:BLUE forState:UIControlStateNormal];
        [btn setBackgroundColor:[HXColor colorWithHexString:@"87CEFA"] forState:UIControlStateHighlighted];
        btn.layer.cornerRadius = 4;
        [btn setTitle:@"确定" forState:UIControlStateNormal];
        [btn setTitleColor:WHITE forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:17];
        [btn addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
        UIView *bottom = [[UIView alloc]init];
        [view addSubview:bottom];
        UIImageView *safe = [[UIImageView alloc]init];
        [bottom addSubview:safe];
        safe.image = [UIImage imageNamed:@"first_safe"];
        [safe lt_YcenterTo:nil save:YES];
        UILabel *safeLabel = [[UILabel alloc]init];
        [bottom addSubview:safeLabel];
        safeLabel.text = [Sun getUser:SAFETEXT];
        safeLabel.font = [UIFont systemFontOfSize:10];
        safeLabel.textColor = [HXColor colorWithHexString:@"5f606d"];
        [safeLabel lt_YcenterTo:nil save:YES];
        [safeLabel lt_setLeft:X(5) to:safe save:YES];
        [bottom lt_layoutView:bottom side:SunLeft to:safe toSide:SunLeft withSize:1 offset:0 save:NO];
        [bottom lt_layoutView:bottom side:SunRight to:safeLabel toSide:SunRight withSize:1 offset:0 save:YES];
        [bottom lt_XcenterTo:nil save:NO];
        [bottom lt_layoutView:bottom side:SunTop to:nil toSide:SunBottom withSize:1 offset:Y(80) save:NO];
        return view;
    }
    return nil;
}

- (void)acceptDelegate:(UIButton *)btn{
    if (btn.isSelected) {
        btn.selected = NO;
    }else {
        btn.selected = YES;
    }
}

- (void)gotoDelegate{
    SunWebVC *vc = [[SunWebVC alloc]init];
    vc.urlStr = [NSString stringWithFormat:@"%@/Custody/protocol?source=ios",KWAP];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section ==1 && indexPath.row == 0) {
        SunBankListVC *vc = [[SunBankListVC alloc]init];
        [vc getBank:^(NSDictionary *dic) {
            _bank = dic;
            [_img sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KIMG,[_bank objectForKey:@"img"]]]];
            _bankName.text = [_bank objectForKey:@"name"];
            [_tb reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

